
package com.hlkj.pay.controller.openapi.order;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.hlkj.framework.common.pojo.CommonResult;
import com.hlkj.framework.common.util.json.JsonUtils;
import com.hlkj.pay.app.openapi.IOpenApiAppService;
import com.hlkj.pay.app.order.IAcctOrderAppService;
import com.hlkj.pay.app.order.IPayOrderAppService;
import com.hlkj.pay.app.order.IRefundOrderAppService;
import com.hlkj.pay.common.CommonResultCode;
import com.hlkj.pay.vo.openapi.order.req.PayOrderQueryReq;
import com.hlkj.pay.vo.openapi.order.resp.PayOrderSubmitApiResp;
import com.hlkj.pay.vo.order.OpenApiResult;
import com.hlkj.pay.vo.order.req.*;
import com.hlkj.pay.vo.order.resp.PayOrderResp;
import com.hlkj.pay.vo.order.resp.RefundOrderResp;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author HlpayTeam
 * @date 2024/10/09 11:25
 */
@Tag(name = "商户AUT订单管理")
@RestController
@RequestMapping("/openapi/pay")
@RequiredArgsConstructor
@Slf4j
public class OpenApiOrderPayController {

    private final IPayOrderAppService payOrderAppService;

    private final IOpenApiAppService openApiAppService;

    private final IRefundOrderAppService openRefundOrderAppService;

    private final IAcctOrderAppService acctOrderAppService;

    @Operation(summary = "AUT统一下单")
    @PostMapping("/create")
    OpenApiResult<PayOrderSubmitApiResp> submitOrder(@Validated @RequestBody OrderCommonParamReq orderCommonParamReq) {
        try {
            PayOrderParamReq paramReqParams = JsonUtils.parseObject(orderCommonParamReq.getBizContent(), PayOrderParamReq.class);
            CommonResult<PayOrderSubmitApiResp> payOrderRespCommonResult = payOrderAppService.submitOrder(paramReqParams);
            return openApiAppService.sign(payOrderRespCommonResult);
        }
        catch (Exception e) {
            e.printStackTrace();
            return OpenApiResult.error(CommonResultCode.PARAMETER_ERROR.getCode(), e.getMessage());
        }
    }

    @Operation(summary = "AUT订单查询")
    @PostMapping("/query")
    OpenApiResult<PayOrderResp> queryOrder(@RequestBody OrderCommonParamReq orderCommonParamReq) {
        PayOrderQueryReq payOrderQueryReq = JsonUtils.parseObject(orderCommonParamReq.getBizContent(), PayOrderQueryReq.class);
        CommonResult<PayOrderResp> payOrderRespCommonResult = payOrderAppService.queryOrder(payOrderQueryReq);
        return openApiAppService.sign(payOrderRespCommonResult);
    }

    @Operation(summary = "AUT订单关闭")
    @PostMapping("/close")
    OpenApiResult<Void> closeOrder(@RequestBody OrderCommonParamReq orderCommonParamReq) {
        PayOrderQueryReq payOrderQueryReq = JsonUtils.parseObject(orderCommonParamReq.getBizContent(), PayOrderQueryReq.class);
        CommonResult<Void> commonResult = payOrderAppService.closeOrder(payOrderQueryReq);
        return openApiAppService.sign(commonResult);
    }

    @Operation(summary = "AUT订单退款")
    @PostMapping("/refund")
    OpenApiResult<RefundOrderResp> refundOrder(@Validated @RequestBody OrderCommonParamReq orderCommonParamReq) {
        try {
            RefundOrderParamReq payOrderQueryReq = JsonUtils.parseObject(orderCommonParamReq.getBizContent(), RefundOrderParamReq.class);
            CommonResult<RefundOrderResp> refundOrderRespCommonResult = openRefundOrderAppService.refundOrder(payOrderQueryReq);
            return openApiAppService.sign(refundOrderRespCommonResult);
        }
        catch (Exception e) {
            return OpenApiResult.error(CommonResultCode.PARAMETER_ERROR.getCode(), e.getMessage());
        }
    }

    @Operation(summary = "AUT订单退款查询")
    @PostMapping("/refund/query")
    OpenApiResult<RefundOrderResp> queryRefundOrder(@RequestBody OrderCommonParamReq orderCommonParamReq) {
        RefundOrderQueryReq payOrderQueryReq = JsonUtils.parseObject(orderCommonParamReq.getBizContent(), RefundOrderQueryReq.class);
        CommonResult<RefundOrderResp> refundOrderRespCommonResult = openRefundOrderAppService.queryRefundOrder(payOrderQueryReq);
        return openApiAppService.sign(refundOrderRespCommonResult);
    }

}
